package com.maxdu.labs.stt.models.db;

import android.util.Log;

import com.maxdu.labs.stt.constants.Constants;
import com.maxdu.labs.stt.models.TPlan;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;

public class TPlanDBHelper {
	public static final String PARSE_DB_TABLENAME_TPLAN = "tplan";
	public static final String PARSE_DB_FIELD_TPLANID = "tplanId";
	public static final String PARSE_DB_FIELD_NAME = "name";
	public static final String PARSE_DB_FIELD_DURATION = "duration";
	public static final String PARSE_DB_FIELD_TYPE1 = "type1";
	public static final String PARSE_DB_FIELD_TYPE2 = "type2";
	public static final String PARSE_DB_FIELD_TYPE3 = "type3";
	public static final String PARSE_DB_FIELD_TYPE4 = "type4";
	public static final String PARSE_DB_FIELD_FREQUENCY1 = "frequency1";
	public static final String PARSE_DB_FIELD_FREQUENCY2 = "frequency2";
	public static final String PARSE_DB_FIELD_FREQUENCY3 = "frequency3";
	public static final String PARSE_DB_FIELD_FREQUENCY4 = "frequency4";
	
	private ParseObject tplanParseObject;
	//private TPlan training;
	
	public TPlanDBHelper(){
		tplanParseObject = new ParseObject (PARSE_DB_TABLENAME_TPLAN);
	}
	
	public void saveToCloud(final TPlan tplan){
		
		if(tplan != null){
			//ID
			tplanParseObject.put(PARSE_DB_FIELD_TPLANID, tplan.getId());
			//Name
			if(tplan.getName() != null){
				tplanParseObject.put(PARSE_DB_FIELD_NAME, tplan.getName());
			}else{
				Log.w(Constants.LOG_TAG, "TrainingDBHelper::updateToCloud() - updating an training with a getName() == NULL");
			}
			//duration
			tplanParseObject.put(PARSE_DB_FIELD_DURATION, tplan.getDuration());
			//frequency
			tplanParseObject.put(PARSE_DB_FIELD_FREQUENCY1, tplan.getFrequency());
			
			tplanParseObject.saveInBackground(new SaveCallback() {
				
				@Override
				public void done(ParseException e) {
					if (e == null){
						Log.d(Constants.LOG_TAG, "we successfully updated to Cloud the TPlan: " + tplan.getId());
					}else{
						Log.w(Constants.LOG_TAG, "PROBLEM when trying to save the TPlan [" + tplan.getId() + "] to Cloud. errorMessage:: " + e);
					}
					
				}
			});
		}else{
			Log.e(Constants.LOG_TAG, "TPlanDBHelper::saveToCloud() :: TRAINING object IS NULL");
		}
	}
}
